import os
from typing import Any, Callable, Dict, List, Optional

# 环境变量设置
TASKS_FILE = "tasks.json"  # 问题文件
DOC_STRUCTURE_FILE = "doc_structure_remove_section_7_2.json"  # 文档结构文件
CHAPTERS_DIR = "./chapter_contents/"  # 章节文件目录（默认文件名为1.json, 2.json, 3.json, etc.）
EXAMPLE_ASCEND_C_FILE_DIR = "static_prompts"
HARD_CODED_CHAPTER_IDS = ["16.1.4.3.1","16.1.4.4.1"]
OUTPUT_FILE_PREFIX = "./generated_ascend_codes/codes"  # 输出文件
LOG_DIR = "./generated_ascend_codes/logs/"  # 输出文件
SAMPLE_CODE_TASK_DESCRIPTION = ""

MAX_RETRIES = 3  # 最大重试次数
RETRY_DELAY = 5  # 重试延迟(秒)

REPEAT_TIMES = 1

environment_variables: Dict[str, Callable[[], Any]] = {
    "TASKS_FILE": 
    lambda: os.getenv("TASKS_FILE", "tasks.json"),

    "DOC_STRUCTURE_FILE": 
    lambda: os.getenv("DOC_STRUCTURE_FILE", "doc_structure_remove_section_7_2.json"),

    "CHAPTERS_DIR": 
    lambda: os.getenv("CHAPTERS_DIR", "./chapter_contents/"),

    "EXAMPLE_ASCEND_C_FILE_DIR": 
    lambda: os.getenv("EXAMPLE_ASCEND_C_FILE_DIR", "static_prompts"),

    "HARD_CODED_CHAPTER_IDS": 
    lambda: HARD_CODED_CHAPTER_IDS.extend(os.getenv("HARD_CODED_CHAPTER_IDS", "").split(",")),

    "OUTPUT_FILE_PREFIX": 
    lambda: os.getenv("OUTPUT_FILE_PREFIX", "./generated_ascend_codes/codes"),

    "LOG_DIR": 
    lambda: os.getenv("LOG_DIR", "./generated_ascend_codes/logs/"),

    "SAMPLE_CODE_TASK_DESCRIPTION": 
    lambda: os.getenv(
        "SAMPLE_CODE_TASK_DESCRIPTION", 
        "sinh算子接收一个输入张量input，计算过程为对输入张量中的每个元素应用双曲正弦函数：sinh(x) = (e^x - e^(-x))/2。该算子执行逐元素操作，输出张量与输入张量具有完全相同的形状。双曲正弦函数在神经网络中可作为激活函数，在处理具有指数增长特性的数据时特别有用。请实现数值稳定且高效的sinh算子代码，确保处理大值时不会发生溢出。"
    ),

    "MAX_RETRIES": 
    lambda: int(os.getenv("MAX_RETRIES", 3)),

    "RETRY_DELAY": 
    lambda: int(os.getenv("RETRY_DELAY", 5)),

    "REPEAT_TIMES": 
    lambda: int(os.getenv("REPEAT_TIMES", 1)),

}

def __getattr__(name: str):
    # lazy evaluation of environment variables
    if name in environment_variables:
        return environment_variables[name]()
    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


def __dir__():
    return list(environment_variables.keys())